package com.hyt.it.ogt.kq.main.controller.gov;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.vo.basepackage.BaseDataPackageVO;
import com.hyt.it.ogt.kq.service.gov.service.IExamPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 基础包管理
 * </p>
 *
 * @author liuhh
 * @since 2021-03-08
 */
@RestController
@Api(tags = "115.基础包接口", value = "基础包接口")
@ApiSort(value = 115)
@RequestMapping("/kw/gov/baseDataPackage/")
@Slf4j
public class BaseDataPackageController extends BaseController {

    @Resource
    private IExamPackageService iExamPackageService;

    @ApiOperation(
            value = "15.1 生成基础包"
    )
    @ApiOperationSort(value = 1)
    @PostMapping("create")
    public ApiResponse<Object> create(@RequestParam String taskId) {
        log.info("# 生成基础包参数 ： {}", taskId);
        try {
            iExamPackageService.generateBaseDataPackage(taskId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 生成基础包校验不通过: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 生成基础包异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_GENERATE_BASE_DATA_PACKAGE.getCode()).build();
        }
    }

    @ApiOperation(
            value = "15.2 查询基础包"
    )
    @ApiOperationSort(value = 2)
    @GetMapping(value = "query")
    public ApiResponse<BaseDataPackageVO> query(@RequestParam String taskId) {
        log.info("# 查询基础包参数 ： {}", taskId);
        try {
            BaseDataPackageVO vo = iExamPackageService.queryBaseDataPackage(taskId);
            return ApiResponse.<BaseDataPackageVO>builder().code(ResponseCode.SUCCESS.getCode()).data(vo).build();
        } catch (Exception e) {
            log.error("# 查询基础包异常:", e);
            return ApiResponse.<BaseDataPackageVO>builder().code(ResponseCode.ERROR_GET_BASE_DATA_PACKAGE.getCode()).build();
        }
    }

    @ApiOperation(
            value = "15.3 删除基础包"
    )
    @ApiOperationSort(value = 3)
    @DeleteMapping(value = "/deleteByTaskId")
    public ApiResponse deleteByTaskId(@RequestParam String taskId) {
        try {
            iExamPackageService.deleteBaseDataPackageByTaskId(taskId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException k) {
            log.error("# 删除基础包业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 删除基础包异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_DELETE_BASE_DATA_PACKAGE.getCode()).build();
        }
    }

    @ApiOperation(value = "15.4 获取基础包下载URL")
    @ApiOperationSort(value = 4)
    @GetMapping("/getBaseDataPackageUrl")
    public ApiResponse<String> getBaseDataPackageUrl(@RequestParam String taskId) {
        String url = null;
        try {
            url = iExamPackageService.getBaseDataPackageUrl(taskId);
            if (StringUtils.isBlank(url)) {
                return ApiResponse.<String>builder().code(ResponseCode.BASE_DATA_PACKAGE_NOT_EXIST.getCode()).build();
            }
            return ApiResponse.<String>builder().code(ResponseCode.SUCCESS.getCode()).data(url).build();
        } catch (Exception e) {
            return ApiResponse.<String>builder().code(ResponseCode.ERROR_GET_BASE_DATA_PACKAGE_URL.getCode()).build();
        }

    }
}
